package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 发送验证码
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> senMsg(@RequestBody User user, HttpSession session){ // 其实只传过来一个phone
        // 获取手机号
        String phone = user.getPhone();

        if(StringUtils.isNotEmpty(phone)){
            // 生成随机的四位验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code={},",code);
            // 调用阿里云提供的短信服务API完成发送短信。
            // SMSUtils.sendMessage("瑞吉外卖","",phone,code);

            // 将生成的验证码保存到session中
             session.setAttribute(phone,code);
            return R.success("短信发送成功！");
        }
        return R.error("短信发送失败！");

    }

    /**
     * 登录
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session){ // 前端发来phone和code 可以dto接收，也可以map接收

        log.info("map:{}",map.toString()); //map:{phone=13333331111, code=4643}
        // 从map里获取手机号 验证码，
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();

        // 和session中 获取保存得到的验证码进行比对
        Object phoneCode = session.getAttribute(phone);
        if(phoneCode!=null && phoneCode.equals(code)){
            // 登陆成功--->是否在User表中，  不在 则自动完成创建。
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone,phone);
            User user = userService.getOne(queryWrapper);
            if(user == null){
                // 注册
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            session.setAttribute("user",user.getId());

            return R.success(user);

        }
        return R.error("验证码不正确");


    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/loginout")
    public R<String> loginout(HttpSession httpSession){
        // 根据当前id 找phone
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,currentId);
        User user = userService.getOne(queryWrapper);
        httpSession.removeAttribute(user.getPhone());
        return R.success("退出成功");
    }

}
